package com.zhangsun.springShiro.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangsun.springShiro.common.provider.RedisProvider;
import com.zhangsun.springShiro.system.entity.RoleEntity;
import com.zhangsun.springShiro.system.mapper.RoleMapper;
import com.zhangsun.springShiro.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zhangsun
 * @since 2019-07-23
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Autowired
    private RedisProvider redisProvider;

    @Override
    public List<RoleEntity> findRolesByUser(String userId) {
        String rolesObject = (String) redisProvider.get(userId + ":role");
        if (StrUtil.isNotBlank(rolesObject)) {
            return JSON.parseArray(rolesObject, RoleEntity.class);
        }

        List<RoleEntity> roles = baseMapper.findRolesByUser(userId);
        redisProvider.set(userId + ":role", JSON.toJSONString(roles));
        return roles;
    }
}
